package edu.sctu.practice.class01.zhangwang.day2015_10_14;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by Z_W on 2015/10/14.
 */
public class Client {

    public static void main(String[] args) {
        HttpURLConnection urlConnection;
        int responseCode;
        URL url;
        String readLine;
        BufferedReader bufferedReader;

        try {
            //新建一个URL对象，样获取的网页地址为：http://www.baidu.com
            url = new URL("http://demo.mycodes.net/moban/HTML5_user_reg/");
            //打开URL
            urlConnection = (HttpURLConnection)url.openConnection();
            //获取服务器的响应代码
            responseCode = urlConnection.getResponseCode();

            System.out.println("网页的响应代码为："+responseCode);

            if (responseCode == 200){
                //得到输入流，即获得网页的内容
                bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"utf-8"));
                while ((readLine = bufferedReader.readLine()) != null){
                    System.out.println(readLine);
                }
            }else {
                System.out.println("获取不到源代码，服务器响应代码为："+responseCode);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
